<template>
  <div>
    <h1>工作流</h1>
  </div>
</template>
<script setup lang="ts">
//@ts-ignore
import { v4 as uuidv4 } from 'uuid';
const json = `{
  "tableId": 1,
  "workFlowDef": { "name": "合同审批" },
  "directorMaxLevel": 4,
  "flowPermission": [
    {
      "type": 1,
      "targetId": "53128111",
      "name": "雅楠"
    }
  ],
  "nodeConfig": {
    "nodeName": "发起人",
    "type": 0,
    "priorityLevel": "",
    "settype": "",
    "selectMode": "",
    "selectRange": "",
    "directorLevel": "",
    "examineMode": "",
    "noHanderAction": "",
    "examineEndDirectorLevel": "",
    "ccSelfSelectFlag": "",
    "conditionList": [],
    "nodeUserList": [],
    "childNode": {
      "nodeName": "路由",
      "type": 4,
      "childNode": null,
      "conditionNodes": [
        {
          "nodeName": "条件0.1",
          "error": false,
          "type": 3,
          "priorityLevel": 1,
          "conditionList": [
            {
              "showType": "1",
              "columnId": "1090",
              "type": 2,
              "showName": "预计采购总费用",
              "optType": "1",
              "zdy1": "2",
              "opt1": "<",
              "zdy2": "",
              "opt2": "<",
              "columnDbname": "planProcurementTotalMoney",
              "columnType": "Double"
            },
            {
              "showType": "3",
              "columnId": "1092",
              "type": 2,
              "showName": "采购类型",
              "zdy1": "1",
              "columnDbname": "procurementType",
              "columnType": "String",
              "fixedDownBoxValue": ""
            }
          ],
          "nodeUserList": [],
          "childNode": {
            "nodeName": "审核人1",
            "error": false,
            "type": 1,
            "settype": 2,
            "selectMode": 0,
            "selectRange": 0,
            "directorLevel": 1,
            "examineMode": 1,
            "noHanderAction": 2,
            "examineEndDirectorLevel": 0,
            "childNode": {
              "nodeName": "路由",
              "type": 4,
              "childNode": {
                "nodeName": "审核人3",
                "error": false,
                "type": 1,
                "settype": 2,
                "selectMode": 0,
                "selectRange": 0,
                "directorLevel": 1,
                "examineMode": 1,
                "noHanderAction": 2,
                "examineEndDirectorLevel": 0,
                "childNode": null,
                "nodeUserList": []
              },
              "conditionNodes": [
                {
                  "nodeName": "条件1.1",
                  "error": false,
                  "type": 3,
                  "priorityLevel": 1,
                  "conditionList": [
                    {
                      "showType": "1",
                      "columnId": "1093",
                      "type": 2,
                      "showName": "园区面积",
                      "optType": "1",
                      "zdy1": "2",
                      "opt1": "<",
                      "zdy2": "",
                      "opt2": "<",
                      "columnDbname": "parkArea",
                      "columnType": "Double"
                    }
                  ],
                  "nodeUserList": [],
                  "childNode": null
                },
                {
                  "nodeName": "条件1.2",
                  "type": 3,
                  "priorityLevel": 2,
                  "conditionList": [],
                  "nodeUserList": [],
                  "childNode": {
                    "nodeName": "路由",
                    "type": 4,
                    "childNode": {
                      "nodeName": "审核人4",
                      "error": false,
                      "type": 1,
                      "settype": 2,
                      "selectMode": 0,
                      "selectRange": 0,
                      "directorLevel": 1,
                      "examineMode": 1,
                      "noHanderAction": 2,
                      "examineEndDirectorLevel": 0,
                      "childNode": null,
                      "nodeUserList": []
                    },
                    "conditionNodes": [
                      {
                        "nodeName": "条件2.1",
                        "error": false,
                        "type": 3,
                        "priorityLevel": 1,
                        "conditionList": [
                          {
                            "showType": "1",
                            "columnId": "1093",
                            "type": 2,
                            "showName": "园区面积",
                            "optType": "1",
                            "zdy1": "2",
                            "opt1": "<",
                            "zdy2": "",
                            "opt2": "<",
                            "columnDbname": "parkArea",
                            "columnType": "Double"
                          }
                        ],
                        "nodeUserList": [],
                        "childNode": {
                          "nodeName": "审核人2.1",
                          "error": false,
                          "type": 1,
                          "settype": 2,
                          "selectMode": 0,
                          "selectRange": 0,
                          "directorLevel": 1,
                          "examineMode": 1,
                          "noHanderAction": 2,
                          "examineEndDirectorLevel": 0,
                          "childNode": null,
                          "nodeUserList": []
                        }
                      },
                      {
                        "nodeName": "条件2.2",
                        "type": 3,
                        "priorityLevel": 2,
                        "conditionList": [],
                        "nodeUserList": [],
                        "childNode": {
                          "nodeName": "审核人2.2",
                          "error": false,
                          "type": 1,
                          "settype": 2,
                          "selectMode": 0,
                          "selectRange": 0,
                          "directorLevel": 1,
                          "examineMode": 1,
                          "noHanderAction": 2,
                          "examineEndDirectorLevel": 0,
                          "childNode": null,
                          "nodeUserList": []
                        },
                        "error": false
                      }
                    ]
                  },
                  "error": false
                }
              ]
            },
            "nodeUserList": []
          }
        },
        {
          "nodeName": "条件0.2",
          "type": 3,
          "priorityLevel": 2,
          "conditionList": [],
          "nodeUserList": [],
          "childNode": null,
          "error": false
        }
      ]
    },
    "conditionNodes": []
  }
}`;
const data: any = JSON.parse(json);
const trees = data.nodeConfig.childNode;

// 数据对象新增uuid
function addUuidToObjects<T extends object>(obj: T): T | Array<T> {
  if (Array.isArray(obj)) {
    return obj.map((item) => addUuidToObjects(item)) as Array<T>;
  }

  if (typeof obj === 'object' && obj !== null) {
    // Create a shallow copy of the object to avoid modifying the original
    const newObj = { ...obj };

    // Add a UUID to the current object
    (newObj as any).uuid = uuidv4(); // Cast to 'any' to bypass TypeScript error

    // Recursively add UUIDs to child objects and arrays
    for (const key in newObj) {
      if (typeof key === 'string' && Object.prototype.hasOwnProperty.call(newObj, key)) {
        const value = newObj[key];
        if (typeof value === 'object' && value !== null) {
          newObj[key] = addUuidToObjects(value) as any;
        }
      }
    }

    return newObj as T;
  }

  return obj as T; // Return non-object values as-is
}

// 将树扁平化处理
function flattenTree(tree: any, parentId = null) {
  const flatNodes: any = [];
  const currentNode = { ...tree, parentId };
  flatNodes.push(currentNode);
  const processChildNode = (node: any) => {
    if (node.childNode) {
      flatNodes.push({ ...node.childNode, parentId: node.childNode.uuid });
      processChildNode(node.childNode);
    }
    if (node.conditionNodes) {
      node.conditionNodes.forEach((item: any) => {
        flatNodes.push({ ...item, parentId: item.uuid });
        processChildNode(item);
      });
    }
  };
  processChildNode(tree);
  return flatNodes;
}

const dataWithUuids = addUuidToObjects(trees);
console.log(dataWithUuids);

const flattenedDataWithIds = flattenTree(dataWithUuids);
console.log(flattenedDataWithIds);
</script>
